package world.criver.imagecompresstool.function;

import javafx.application.Platform;
import javafx.event.Event;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import world.criver.imagecompresstool.data.daValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 系统托盘
 */
public class funTray {
    /**
     * 初始化托盘
     */
    public void iniTray(){
        try {
            if (!SystemTray.isSupported()) return;
            SystemTray tray = SystemTray.getSystemTray();
            Image icon = Toolkit.getDefaultToolkit().createImage(funTray.class.getResource("/world/criver/imagecompresstool/material/ico_32x32.png"));
            TrayIcon ico = new TrayIcon(icon, "图片处理工具");
            ico.setImageAutoSize(true);
            tray.add(ico);
            ico.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(e.getButton()==3){
                        SwingUtilities.invokeLater(()->{daValue.openPop(e.getXOnScreen(),e.getYOnScreen());});
                    }
                }
                @Override
                public void mousePressed(MouseEvent e) {
                    if(e.getButton()!=1)return;
                    long a=System.currentTimeMillis();
                    if(a- daValue.click_time<500){
                        SwingUtilities.invokeLater(daValue::openMaster);
                    }else daValue.click_time=a;
                }
                @Override
                public void mouseReleased(MouseEvent e) {}
                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {}
            });
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 初始化右键菜单
     */
    public void iniPopMenu(){
        Platform.runLater(()->{
            Stage pa=new Stage();
            pa.initStyle(StageStyle.UTILITY);
            pa.setOpacity(0);
            Stage pop=new Stage();
            VBox vb=new VBox();
            vb.getStylesheets().add(funTray.class.getResource("/world/criver/imagecompresstool/css/context_menu.css").toString());
            vb.getStyleClass().add("vbox");
            Label l1=new javafx.scene.control.Label("打开");
            Label l2=new javafx.scene.control.Label("退出");
            l1.getStyleClass().add("label");
            l2.getStyleClass().add("label");
            vb.getChildren().add(l1);
            vb.getChildren().add(l2);
            Scene sc=new Scene(vb);
            sc.setFill(null);
            pop.setScene(sc);
            pop.initStyle(StageStyle.TRANSPARENT);
            pop.initOwner(pa);
            pa.setAlwaysOnTop(true);
            pop.focusedProperty().addListener((a,o,n)->{
                if(!n){
                    daValue.hidePop();
                }
            });
            l1.setOnMouseClicked(e->{daValue.openMaster();});
            l2.setOnMouseClicked(e->{
                new funHotKey().removeHook();
                System.exit(0);});
            daValue.pop_parent=pa;
            daValue.pop_menu=pop;
        });
    }

    /**
     * javafx保活
     */
    public void keepAlive(){
        Platform.runLater(()->{
            Stage pa=new Stage();
            pa.setWidth(1);
            pa.setHeight(1);
            pa.setX(-100);
            pa.setY(-100);
            pa.setOpacity(0);
            pa.initStyle(StageStyle.UTILITY);
            BorderPane bp=new BorderPane();
            Scene sc=new Scene(bp);
            sc.setFill(null);
            Stage ch=new Stage();
            ch.setScene(sc);
            ch.setWidth(1);
            ch.setHeight(1);
            ch.setX(-100);
            ch.setY(-100);
            ch.initStyle(StageStyle.TRANSPARENT);
            ch.initOwner(pa);
            pa.show();
            ch.show();
        });
    }
}
